<?php

namespace SinoPHP\BbdBoss\Api;

/**
 * 设备信息同步接口
 *
 * Params:
 *      data:startTime    (int)     查询起始时间
 *      data:endTime      (int)     查询结束时间
 */
class DeviceInfo extends ApiAbstract {

    public function v_1_0_0() {
        $params = $this->requestParams;

        //$this->checkToken(); // 校验TOKEN
        $where = [];
        if (isset($params['startTime']) && isset($params['endTime'])) {
            $where = ['update_time|<>' => [$params['startTime'], $params['endTime']]];
        }
        $fields = 'hostname,throughput,device_id,status';
        $deviceData = model('DeviceInfo')->getList($fields, $where);

        $ipData = model('ServerIp')->getList('device_number_id,isp,hostname,ip,is_delete,status',['belong_type'=>0]);
        $ipSortList = [];
        foreach ($ipData as $item) {
            $rowData['is_delete'] = 1;
            if ($item['is_delete'] == 0 && $item['status'] == 1) {
                $rowData['is_delete'] = 0;
            }

            $rowData['ip'] = $item['ip'];
            $rowData['isp'] = $item['isp'];
            $ipSortList[$item['hostname']][] = $rowData;
        }

        foreach ($deviceData as $key => $value) {
            $deviceData[$key]['ips'] = $ipSortList[$value['hostname']] ? : [];
            $deviceData[$key]['throughput'] = (int)$value['throughput'];
            $deviceData[$key]['status'] = (int)$value['status'];
        }

        $this->success($deviceData);
    }

}